package com.kucai.yifaqapp.widget;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.ResSetbet;
import com.kucai.yifaqapp.model.network.entity.res.gendanBean;
import com.kucai.yifaqapp.util.OnItemClickListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static com.kucai.yifaqapp.global.App.getContext;

/**
 * Created by admin on 2018/12/26.
 */

public class GendanDialog extends Activity {
    private ImageView gameimg;
    private TextView gameuser;
    private TextView gameinfo;
    private RecyclerView lists;
    private TextView gamejine;
    private Button conform;
    private ImageView close;
    private String gendaninfo;
    private String gendanConform;
    private String gendanUser;
    List<String> mbetBean_Play = new ArrayList<>();
    List<String> mbetBean_odds = new ArrayList<>();
    List<String> mbetBean_money = new ArrayList<>();
    List<String> mbetBean_rebate = new ArrayList<>();
    List<String> mbetBean_betinfo = new ArrayList<>();
    private String gameId;
    private String turnNum;
    private String totalNums;
    private String totalMoney;
    private String betSrc;
    private String zhunumber;
    private RelativeLayout content;
    private int gameid;
    private int xiazhuType = 0;// 0是普通数据  1是六合彩自选不中  2是快乐十分连码
    private int totalnums;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gengdan_dialog);
        gendaninfo = getIntent().getStringExtra("gendaninfo");
        gendanConform = getIntent().getStringExtra("gendanConform");
        gendanUser = getIntent().getStringExtra("gendanUser");
        initview();
        initRecyclerView();
        initOthers();
        initConformData();//初始化提交的数据

//        typedef enum : int  {
//            TP_XGLHC = 70,//香港六合彩
//                    TP_CQSSC = 1,//重庆时时彩
//                    TP_GDKLSF = 60,//广东快乐十分
//                    TP_GDSYXW = 21,//广东11选5
//                    TP_MSSSC = 81,//秒速时时彩
//                    TP_PCDD = 66,//PC蛋蛋
//                    TP_MSSC = 80,//秒速赛车
//                    TP_XJSSC = 4,//新疆时时彩
//                    TP_MSFT = 82,//秒速飞艇 幸运飞艇
//                    TP_CQXYNC = 61,//重庆幸运农场
//                    TP_BJPKS = 50,//北京快乐10/北京赛车
//                    TP_BJKL = 65,//北京快乐8
//                    TP_XYLHC = 85,//幸运六合彩
//                    TP_FCSD = 30,//福彩3D
//                    TP_MSNN = 91,//秒速牛牛
//                    TP_PKSNN = 90,//PK10牛牛
//                    TP_XYKL = 83,//幸运快乐8
//                    TP_XYDD = 84,//幸运蛋蛋
//                    TP_JSKS = 10,//江苏快3
//                    TP_BJKS = 11,//安徽快3
//                    TP_GXKS = 12,//广西快3
//                    TP_HBKS = 13,//湖北快3
//                    TP_MSKS = 86,//秒速快3
//                    TP_PM = 99,//跑马
//                    TP_TJSSC = 5,//天津时时彩
//                    TP_HEBKS = 15,//河北快3
//                    TP_GANSKS = 16,//甘肃快3
//                    TP_GUIZKS = 18,//贵州快3
//                    TP_TXFFC = 113,//QQ分分彩
//                    TP_MSQXC = 114,//秒速七星彩
//        } CPType;
    }

    private void initConformData() {
        try {
//            //六合彩自选不中
            if ((gameid == 70 || gameid == 80)) {
                try {
                    gendanBean bean = new Gson().fromJson(gendaninfo, gendanBean.class);
                    if (bean.getPlayArr().size() != 0) {
                        if (bean.getPlayArr().get(0).getPlayName().contains("自选不中")
                                ) {
                            xiazhuType = 1;
                        }
                    }
                } catch (Exception e) {

                }
            }
            if ((gameid == 61 || gameid == 21 || gameid == 60)) {
                try {
                    gendanBean bean = new Gson().fromJson(gendaninfo, gendanBean.class);
                    if (bean.getPlayArr().size() != 0) {
                        if (bean.getPlayArr().get(0).getPlayName().contains("连码")) {
                            xiazhuType = 2;
                        }
                    }
                } catch (Exception e) {

                }
            }
            String text1 = gendanConform.replace("{", "");
            String text2 = text1.replace("}", "");
            String[] text3 = text2.split(",");
            for (int i = 0; i < text3.length; i++) {
                if (text3[i].contains("betBean_playId")) {
                    String[] strings = text3[i].split(":");
                    mbetBean_Play.add(strings[1].replace("\"", ""));
                } else if (text3[i].contains("betBean_odds")) {
                    String[] strings = text3[i].split(":");
                    mbetBean_odds.add(strings[1].replace("\"", ""));
                } else if (text3[i].contains("betBean_money")) {
                    String[] strings = text3[i].split(":");
                    mbetBean_money.add(strings[1].replace("\"", ""));
                } else if (text3[i].contains("betBean_rebate")) {
                    String[] strings = text3[i].split(":");
                    mbetBean_rebate.add(strings[1].replace("\"", ""));
                } else if (text3[i].contains("gameId")) {
                    String[] strings = text3[i].split(":");
                    gameId = strings[1].replace("\"", "");
                } else if (text3[i].contains("turnNum")) {
                    String[] strings = text3[i].split(":");
                    turnNum = strings[1].replace("\"", "");
                } else if (text3[i].contains("totalNums")) {
                    String[] strings = text3[i].split(":");
                    totalNums = strings[1].replace("\"", "");
                } else if (text3[i].contains("totalMoney")) {
                    String[] strings = text3[i].split(":");
                    totalMoney = strings[1].replace("\"", "");
                } else if (text3[i].contains("betSrc")) {
                    String[] strings = text3[i].split(":");
                    betSrc = strings[1].replace("\"", "");
                } else if (text3[i].contains("betBean_betinfo")) {
//                    String[] strings = text3[i].split(":");
//                    mbetBean_betinfo.add(strings[1].replace("\"", ""));
                    //不使用分割的数据源，直接使用jsonobject解析
                    JSONObject object = null;
                    try {
                        object = new JSONObject(gendanConform);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    for (int k = 0; k < totalnums; k++) {
                        String index = "betBean_betinfo[" + k + "]";
                        String strs = null;
                        try {
                            strs = object.getString(index);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        mbetBean_betinfo.add(strs);
                    }
                }
            }
        } catch (Exception e) {

        }
    }

    private void initOthers() {
        try {
            gendanBean bean = new Gson().fromJson(gendaninfo, gendanBean.class);
            gameid = bean.getGameId();
            totalnums = bean.getTotalNums();
            gameuser.setText(gendanUser + "");
            gamejine.setText("共" + bean.getTotalNums() + "注， 共" + bean.getTotalMoney() + " 元");
            zhunumber = bean.getTotalNums() + "";
            switch (bean.getGameId()) {
                case 80:
                    Glide.with(getContext()).load(R.mipmap.mssc).into(gameimg);
                    gameinfo.setText("秒速赛车 第" + bean.getIssue() + "期投注记录");
                    break;
                case 55:
                    Glide.with(getContext()).load(R.mipmap.xyft).into(gameimg);
                    gameinfo.setText("幸运飞艇 第" + bean.getIssue() + "期投注记录");
                    break;
                case 50:
                    Glide.with(getContext()).load(R.mipmap.bjsc).into(gameimg);
                    gameinfo.setText("北京赛车 第" + bean.getIssue() + "期投注记录");
                    break;
                case 82:
                    Glide.with(getContext()).load(R.mipmap.msft).into(gameimg);
                    gameinfo.setText("秒速飞艇 第" + bean.getIssue() + "期投注记录");
                    break;
                case 99:
                    Glide.with(getContext()).load(R.mipmap.xgpm).into(gameimg);
                    gameinfo.setText("香港跑马 第" + bean.getIssue() + "期投注记录");
                    break;
                case 70:
                    Glide.with(getContext()).load(R.mipmap.xglhc).into(gameimg);
                    gameinfo.setText("香港六合彩 第" + bean.getIssue() + "期投注记录");
                    break;
                case 81:
                    Glide.with(getContext()).load(R.mipmap.msssc).into(gameimg);
                    gameinfo.setText("秒速时时彩 第" + bean.getIssue() + "期投注记录");
                    break;
                case 1:
                    Glide.with(getContext()).load(R.mipmap.cqssc).into(gameimg);
                    gameinfo.setText("重庆时时彩 第" + bean.getIssue() + "期投注记录");
                    break;
                case 4:
                    Glide.with(getContext()).load(R.mipmap.xjssc).into(gameimg);
                    gameinfo.setText("新疆时时彩 第" + bean.getIssue() + "期投注记录");
                    break;
                case 60:
                    Glide.with(getContext()).load(R.mipmap.gdklsf).into(gameimg);
                    gameinfo.setText("广东快乐十分 第" + bean.getIssue() + "期投注记录");
                    break;
                case 91:
                    Glide.with(getContext()).load(R.mipmap.msnn).into(gameimg);
                    gameinfo.setText("秒速牛牛 第" + bean.getIssue() + "期投注记录");
                    break;
                case 90:
                    Glide.with(getContext()).load(R.mipmap.pksnn).into(gameimg);
                    gameinfo.setText("PK10牛牛 第" + bean.getIssue() + "期投注记录");
                    break;
                case 61:
                    Glide.with(getContext()).load(R.mipmap.cqxync).into(gameimg);
                    gameinfo.setText("重庆幸运农场 第" + bean.getIssue() + "期投注记录");
                    break;
                case 66:
                    Glide.with(getContext()).load(R.mipmap.pcdd).into(gameimg);
                    gameinfo.setText("PC蛋蛋 第" + bean.getIssue() + "期投注记录");
                    break;
                case 65:
                    Glide.with(getContext()).load(R.mipmap.bjklb).into(gameimg);
                    gameinfo.setText("北京快乐8 第" + bean.getIssue() + "期投注记录");
                    break;
                case 86:
                    Glide.with(getContext()).load(R.mipmap.msks).into(gameimg);
                    gameinfo.setText("秒速快3 第" + bean.getIssue() + "期投注记录");
                    break;
                case 85:
                    Glide.with(getContext()).load(R.mipmap.xylhc).into(gameimg);
                    gameinfo.setText("幸运六合彩 第" + bean.getIssue() + "期投注记录");
                    break;
                case 10:
                    Glide.with(getContext()).load(R.mipmap.jsks).into(gameimg);
                    gameinfo.setText("江苏快三 第" + bean.getIssue() + "期投注记录");
                    break;
                case 12:
                    Glide.with(getContext()).load(R.mipmap.gxks).into(gameimg);
                    gameinfo.setText("广西快三 第" + bean.getIssue() + "期投注记录");
                    break;
                case 13:
                    Glide.with(getContext()).load(R.mipmap.hubks).into(gameimg);
                    gameinfo.setText("湖北快三 第" + bean.getIssue() + "期投注记录");
                    break;
                case 11:
                    Glide.with(getContext()).load(R.mipmap.ahks).into(gameimg);
                    gameinfo.setText("安徽快三 第" + bean.getIssue() + "期投注记录");
                    break;
                case 16:
                    Glide.with(getContext()).load(R.mipmap.gsks).into(gameimg);
                    gameinfo.setText("甘肃快三 第" + bean.getIssue() + "期投注记录");
                    break;
                case 15:
                    Glide.with(getContext()).load(R.mipmap.hbks).into(gameimg);
                    gameinfo.setText("河北快三 第" + bean.getIssue() + "期投注记录");
                    break;
                case 18:
                    Glide.with(getContext()).load(R.mipmap.gzks).into(gameimg);
                    gameinfo.setText("贵州快三 第" + bean.getIssue() + "期投注记录");
                    break;
                case 21:
                    Glide.with(getContext()).load(R.mipmap.gdsyxw).into(gameimg);
                    gameinfo.setText("广东11选5 第" + bean.getIssue() + "期投注记录");
                    break;
                case 112:
                    Glide.with(getContext()).load(R.mipmap.txffc).into(gameimg);
                    gameinfo.setText("腾讯分分彩 第" + bean.getIssue() + "期投注记录");
                    break;
                case 113:
                    Glide.with(getContext()).load(R.mipmap.qqffc).into(gameimg);
                    gameinfo.setText("QQ分分彩 第" + bean.getIssue() + "期投注记录");
                    break;
                case 801:
                    Glide.with(getContext()).load(R.mipmap.qqffc).into(gameimg);
                    gameinfo.setText("快速赛车 第" + bean.getIssue() + "期投注记录");
                    break;
                case 802:
                    Glide.with(getContext()).load(R.mipmap.qqffc).into(gameimg);
                    gameinfo.setText("快速飞艇 第" + bean.getIssue() + "期投注记录");
                    break;
                case 803:
                    Glide.with(getContext()).load(R.mipmap.qqffc).into(gameimg);
                    gameinfo.setText("快速时时彩 第" + bean.getIssue() + "期投注记录");
                    break;
                case 901:
                    Glide.with(getContext()).load(R.mipmap.sfsc).into(gameimg);
                    gameinfo.setText("三分赛车 第" + bean.getIssue() + "期投注记录");
                    break;
                case 902:

                    Glide.with(getContext()).load(R.mipmap.sfssc).into(gameimg);
                    gameinfo.setText("三分时时彩 第" + bean.getIssue() + "期投注记录");
                    break;
                case 903:
                    Glide.with(getContext()).load(R.mipmap.jslhc).into(gameimg);
                    gameinfo.setText("极速六合彩 第" + bean.getIssue() + "期投注记录");
                    break;
                case 904:
                    Glide.with(getContext()).load(R.mipmap.sflhc).into(gameimg);
                    gameinfo.setText("三分六合彩 第" + bean.getIssue() + "期投注记录");
                    break;
                case 804:
                    Glide.with(getContext()).load(R.mipmap.twxyft).into(gameimg);
                    gameinfo.setText("台灣幸運飛艇 第" + bean.getIssue() + "期投注记录");
                    break;
                default:
                    gameinfo.setVisibility(View.GONE);
                    break;

            }
        } catch (Exception e) {

        }
    }

    private void initRecyclerView() {
        lists.setLayoutManager(new LinearLayoutManager(this));
        Adapter adapter = new Adapter();
        gendanBean bean = new Gson().fromJson(gendaninfo, gendanBean.class);
        adapter.setDatas(bean);
        if (bean != null) {
            if (bean.getPlayArr() != null) {
                if (bean.getPlayArr().size() != 0) {
                    lists.setAdapter(adapter);
                }
            }
        }
    }

    BaseLoadingDialog dialog;

    private void initview() {
        gameimg = findViewById(R.id.gameimg);
        gameuser = findViewById(R.id.gameuser);
        gameinfo = findViewById(R.id.gameinfo);
        lists = findViewById(R.id.lists);
        gamejine = findViewById(R.id.gamejine);
        conform = findViewById(R.id.conform);
        close = findViewById(R.id.close);
        content = findViewById(R.id.content);
        dialog = new BaseLoadingDialog(GendanDialog.this, R.style.CustomDialog);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //确认跟投
        conform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //开始提交数据
                conformData();
            }
        });

    }

    private void conformData() {
//        try {
        if (dialog != null) {
            dialog.show();
        }
        JSONObject result = null;
        try {
            result = new JSONObject();
            result.put("gameId", gameId);
            result.put("turnNum", turnNum);
            result.put("totalNums", totalNums);
            result.put("totalMoney", totalMoney);
            result.put("betSrc", betSrc);
            //新建3个数组
            for (int i = 0; i < mbetBean_money.size(); i++) {
                result.put("betBean_playId[" + i + "]", mbetBean_Play.get(i));
                result.put("betBean_odds[" + i + "]", mbetBean_odds.get(i));
                result.put("betBean_money[" + i + "]", mbetBean_money.get(i));
                result.put("betBean_rebate[" + i + "]", mbetBean_rebate.get(i));
                if (xiazhuType == 1 || xiazhuType == 2) {
                    result.put("betBean_betinfo[" + i + "]", mbetBean_betinfo.get(i));
                }
            }
        } catch (Exception e) {

        }
        HttpJsonUtils.getHttpApiInstance()
                .Betbet(HttpAdress.getuseAdress() + "api/m/lottery/bet.do", result.toString(), Info.user.token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResSetbet>() {
                    @Override
                    protected void onCustomNext(ResSetbet resOods) {
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                        if ("200".equals(resOods.code)) {
                            Toast.makeText(GendanDialog.this, "下注成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(GendanDialog.this, resOods.msg, Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                    }
                });
//        } catch (Exception e) {
//            Log.e("xdh","----------"+e.toString());
//            if (dialog != null) {
//                dialog.dismiss();
//            }
//        }
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        private gendanBean mbeans;

        @Override
        public Adapter.MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Adapter.MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.gengdanitem2, parent, false));
        }

        public void setDatas(gendanBean beans) {
            this.mbeans = beans;
        }

        @Override
        public void onBindViewHolder(final Adapter.MViewHolder holder, final int position) {
            holder.number.setText(position + "");
            holder.name.setText(mbeans.getPlayArr().get(position).getPlayName() + "");
            holder.jine.setText(mbeans.getPlayArr().get(position).getBetBean_money() + "");
            holder.jine.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    mbetBean_money.set(position, s.toString());
                    //重新设置钱的数量
                    int i = 0;
                    for (int x = 0; x < mbetBean_money.size(); x++) {
                        if (!mbetBean_money.get(x).isEmpty()) {
                            i = i + Integer.parseInt(mbetBean_money.get(x));
                        }
                    }
                    gamejine.setText("共" + zhunumber + "注， 共" + i + " 元");
                }
            });
        }

        @Override
        public int getItemCount() {
            return mbeans.getPlayArr().size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView number;
            TextView name;
            EditText jine;

            MViewHolder(View itemView) {
                super(itemView);
                number = itemView.findViewById(R.id.number);
                name = itemView.findViewById(R.id.name);
                jine = itemView.findViewById(R.id.jine);
            }
        }
    }
}
